package nw_pool_size;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

import com.herohead.Misc;

public class nw_pool_size {

	static BufferedReader br;
	static String line = "";
	static Jedis jd;
	static Transaction tx;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String inputFile = args[0];
		//String unit = args[1];
		try {
			br = new BufferedReader(new FileReader(inputFile));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		String redisHost = "172.18.38.14";
		jd = new Jedis(redisHost);

		Jedis jdpl = new Jedis(redisHost);
		tx = jdpl.multi();
		
		
		
		String poolStr = "";
		String volumeStr = "";
		String sizeStr = "";
		
		Misc m = new Misc();
		
		jd.del("t:vol:total");
		cleanupKeys(jd,"t:pool:keys");
		//System.exit(0);
		
		//jd.sadd("t:pool:" + poolStr, volumeStr.trim());
		
		
		try {
			line = br.readLine();
			while (line != null) {
				
				if (line.contains("volume")) {line = br.readLine(); continue;}
				line  = line.trim().replaceAll("\\s+", " ");
				//System.out.println(line);
				
				String[] s = line.split(" ");
				volumeStr = s[0].trim();
				sizeStr = s[1] + " " + s[2];
				
				Long l = m.convertToMBytes(sizeStr);
				
				sizeStr = l.toString();
				//System.out.println(s[2]);
				
				// get pool
				poolStr = "";
				for (int i=3; i < s.length; i++) {
					//System.out.print(s[i]);
					poolStr = poolStr + s[i] + " ";
					
				}
				poolStr = poolStr.trim();
				
				tx.zadd("t:vol:total", l, volumeStr.trim());
				tx.sadd("t:pool:keys", "t:pool:" + poolStr.trim());
				tx.sadd("t:pool:" + poolStr.trim(), volumeStr.trim());
				
				
				//jd.sadd(volumeStr, members)
				
				System.out.println(volumeStr.trim());
				System.out.println(sizeStr.trim());
				System.out.println(poolStr.trim());
				
				
				
				
				
				line = br.readLine();
				
			}
			
			tx.exec();
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	public static  void cleanupKeys(Jedis jd,String key) {
		Set<String> licAppList  = jd.smembers(key);

		for (String verKey : licAppList ) {
			jd.del(verKey);
		}

		jd.del(key);
		
	}

}
